package com.happyclub.paul.service.impl;

import com.happyclub.paul.dao.AdviceShenheRepostirory;
import com.happyclub.paul.dao.UserAdviceRepostitory;
import com.happyclub.paul.dao.UserRepostitory;
import com.happyclub.paul.service.IUserAdviceService;
import com.happyclub.pojo.TbAdviceShenhe;
import com.happyclub.pojo.TbUser;
import com.happyclub.pojo.TbUserAdvice;
import com.happyclub.utils.JsonResult;
import com.happyclub.utils.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.JobSheets;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 魏泉琦
 * @Date: 2019/4/4 09:25
 * @Description: 意见反馈
 */
@Service
public class UserAdviceServiceImpl implements IUserAdviceService {

    @Autowired
    private UserAdviceRepostitory userAdviceRepostitory;


    @Autowired
    private UserRepostitory userRepostitory;


    @Autowired
    private AdviceShenheRepostirory adviceShenheRepostirory;


    /**
     *@Description: 保存用户的反馈意见内容
     *@Author: 魏泉琦
     *@date: 2019/4/4
     */
    @Transactional
    @Override
    public JsonResult saveUserAdviceContent(String content, String userId,String userTepe) {
        Integer cnt = userAdviceRepostitory.saveUserAdviceContent(Uuid.getUUID(), content, userId, userTepe);
        JsonResult jsonResult = new JsonResult();
        if(cnt>0){
            return jsonResult;
        }
        jsonResult.setMsg("网络波动，暂时无法提交");
        jsonResult.setErrorCode("500");
        return jsonResult;
    }

    /**
     *@Description: 根据用户意见的处理状态查询用户意见
     *@Author: 魏泉琦
     *@date: 2019/4/12
     */
    @Override
    public JsonResult findALlUserAdvice(long doStatus) {
        JsonResult jsonResult = new JsonResult();
        try{
            List<TbUserAdvice> userAdvices = new ArrayList<>();
            if (doStatus==0) {         //未处理的用户意见
                userAdvices =  userAdviceRepostitory.findTbUserAdvicesByDoStatus(doStatus);
            }
            if(doStatus==1){           //已处理的用户意见
                userAdvices =  userAdviceRepostitory.findTbUserAdvicesByDoStatus(doStatus);
            }
            if(null==userAdvices || userAdvices.size()==0){
                jsonResult.setMsg("暂无数据");
                jsonResult.setErrorCode("NO");
                return jsonResult;
            }
            //表示集合中存在数据         遍历集合查询出意见所属用户的具体用户信息
            List<TbUserAdvice> tbUserAdvices = new ArrayList<>();
            for(TbUserAdvice userAdvice:userAdvices){
                String userId = userAdvice.getUserId();
                TbUser user = userRepostitory.queryUserById(userId);
                userAdvice.setUser(user);
                tbUserAdvices.add(userAdvice);
            }

            jsonResult.setMsg("已查出数据");
            jsonResult.setErrorCode("OK");
            jsonResult.setResult(tbUserAdvices);
        }catch (Exception e){
            jsonResult.setMsg("网络异常");
            jsonResult.setErrorCode("error");
            e.printStackTrace();
            return jsonResult;
        }
        return jsonResult;
    }

    /**
     *@Description: 修改用户意见信息级别和后期跟踪
     *@Author: 魏泉琦
     *@date: 2019/4/12
     */
    @Transactional
    @Override
    public JsonResult updateUserAdvicesByAdviceId(TbUser user,String replyContent,long infoLovel, long isUpperTracker, long shenHeStatus,String adviceId) {
        JsonResult jsonResult = new JsonResult();
        try{
            Integer cnt  = userAdviceRepostitory.updateUserAdvicesByAdviceId(infoLovel, isUpperTracker,shenHeStatus, adviceId);
            if(cnt<=0){  //修改失败
                jsonResult.setErrorCode("NO");
                jsonResult.setMsg("网络波动");
                return jsonResult;
            }
            //修改成功    插入用户意见反馈处理中间表
            Integer ct = adviceShenheRepostirory.createAdviceShenHeMsg(Uuid.getUUID(), adviceId, user.getUserId(), replyContent, user.getTelephone());
            if(cnt<=0){  //用户意见反馈处理表插入失败
                jsonResult.setErrorCode("NO");
                jsonResult.setMsg("网络波动");
                return jsonResult;
            }
        }catch (Exception e){
            jsonResult.setErrorCode("error");
            jsonResult.setMsg("异常");
            e.printStackTrace();
            return jsonResult;
        }
        return jsonResult;
    }
}
